/*
 * Table.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.gui.elements;

import javax.swing.JTable;
import javax.swing.table.TableColumn;

/**
 *
 * @author <a href="paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class Table extends JTable implements ColumnPositions {
    
    // <editor-fold defaultstate="collapsed" desc=" Creates a new instance of Table ">
    public Table() {
        super();
    }
    // </editor-fold>
    
    /**
     Steps to update cell:
     
     1. need row and column numbers from model as method params
     2. convert row and column to their position in view
     3. calculate x and y based on rowHeight and columnWidth
     4. get height and width from column
     5. invoke repaint(long tm, int x, int y, int width, int height)
     * @param row row number to update
     * @param col column to be updated, from ColumnPositions
     */
    public void repaintCell(int row, int col) {
        
        // calculate row index in view, and if it is unvisible - finish:
        int rowViewIndex = convertRowIndexToView(row);
        if (rowViewIndex < 0) {
            return;
        }
        
        /*
         getting every column to check for it's model index.
         if model index lower - add width of this column to x (to skip it)
         if model index == col - save width of this column.
         */
        int x = 0;
        int width = 0;
        if (col == COL_ALL) {
            width = columnModel.getTotalColumnWidth();
        }
        else {
            for (int i = 0; i < columnModel.getColumnCount(); i++) {
                TableColumn column = columnModel.getColumn(i);
                if (column.getModelIndex() == col) {
                    width = column.getWidth();
                    break;
                }
                x += column.getWidth();
            }
        }
        if (width <= 0) {
            return;
        }

        int height = getRowHeight();

        repaint(0, x, rowViewIndex * height, width, height);
    }
    
    /**
     * method to repaint some columns from row
     * <b>WARNING:</b> cols array MUST be sorted before putting here
     * @param row 
     * @param cols 
     */
    public void repaintCells(int row, int... cols) {
        
        // calculate row index in view, and if it is unvisible - finish:
        int rowViewIndex = convertRowIndexToView(row);
        if (rowViewIndex < 0) {
            return;
        }
        
        int height = getRowHeight();
        int y = rowViewIndex * height;
        
        /*
         getting every column to check for it's model index.
         if model index lower - add width of this column to x (to skip it)
         if model index == col - save width of this column.
         */
        if (cols != null && cols.length > 0) {
            int x = 0;
            for (int i = 0; i < columnModel.getColumnCount(); i++) {
                TableColumn column = columnModel.getColumn(i);
                for (int j = 0; j < cols.length; j++) {
                    if (column.getModelIndex() == cols[j]) {
                        repaint(0, x, y, column.getWidth(), height);
                    }
                }
                x += column.getWidth();
            }
        }
    }
    
}
